package com.maycari.captainbi.util;

import com.alibaba.fastjson.JSON;
import com.maycari.http.util.HttpUtil;
import com.maycari.tool.utils.DigestUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.Optional;
import java.util.Random;
import java.util.TreeMap;

/**
 * @Author: 王宏伟
 * @DateTime: 2021/3/31 11:28
 * @Description: 船长api
 */
@Slf4j
public class CaptainBIUtil {

    private static String api_key = "3bf50feb31b0a3eb8cf1ab9728cb28a0";

    private static String api_secret = "097752b24c892deb28fc69da4fe72df7";

    public static String sendRequest(String action, TreeMap<String, String> params) {
        String nonce_str = getRandomString(16);
        StringBuilder sign = new StringBuilder();

        params.put("api_key", api_key);
        params.put("nonce_str", nonce_str);
        params.put("timestamp", String.valueOf(System.currentTimeMillis()).substring(0, 10));

        Optional.ofNullable(params).get().forEach((k, v) -> {
            sign.append(k).append("=").append(v).append("&");
        });

        StringBuilder apisign = new StringBuilder(sign);
        apisign.append("api_secret=").append(api_secret);
        String api_signkey = DigestUtil.md5Hex(apisign.toString()).toUpperCase();
        StringBuilder url = new StringBuilder(action);
        url.append("&").append(sign).append("&api_signkey=").append(api_signkey);
        String result = HttpUtil.get(url.toString(), null);
        log.info("调用船长api接口：{} ,返回：{}", url, JSON.parseObject(result).toJSONString());
        return result;
    }


    public static String sendRequest(String action, TreeMap<String, String> params, String key, String secret) {
        String nonce_str = getRandomString(16);
        StringBuilder sign = new StringBuilder();

        params.put("api_key", key);
        params.put("nonce_str", nonce_str);
        params.put("timestamp", String.valueOf(System.currentTimeMillis()).substring(0, 10));

        Optional.ofNullable(params).get().forEach((k, v) -> {
            sign.append(k).append("=").append(v).append("&");
        });

        StringBuilder apisign = new StringBuilder(sign);
        apisign.append("api_secret=").append(secret);
        String api_signkey = DigestUtil.md5Hex(apisign.toString()).toUpperCase();
        StringBuilder url = new StringBuilder(action);
        url.append("&").append(sign).append("&api_signkey=").append(api_signkey);
        String result = HttpUtil.get(url.toString(), null);
        log.info("调用船长api接口：{} ,返回：{}", url, JSON.parseObject(result).toJSONString());
        return result;
    }

    public static String getRandomString(int length) {
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(str.length());
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }
}
